// @ts-check
"use strict";

const path = require("node:path");
const { rspack } = require("@rspack/core");

/**
 * @power ms-vscode.ts-file-path-support
 * @template TBaseDir
 * @typedef {string & { baseDir?: TBaseDir }} RelativeFilePath
 */
/**
 * @param {RelativeFilePath<'$dir'>} p
 * @returns {string}
 */
const pwd2 = (p) => path.resolve(__dirname, p);

const isDevelopment = process.env.NODE_ENV !== "production";

/**
 * @type {import("webpack").Configuration}
 */
module.exports = {
  mode: isDevelopment ? "development" : "production",
  target: "web",
  entry: {
    // 统一打包的类库
    vendorNg: {
      import: ["angular-loader", "angular"],
    },
  },
  output: {
    path: path.join(__dirname, "prebundle"),
    filename: "[name]-[contenthash:8].dll.js",
    clean: true,
  },
  resolve: {
    alias: {
      angular: isDevelopment
        ? pwd2("./node_modules/angular/angular.js")
        : pwd2("./node_modules/angular/angular.min.js"),
      "angular-loader": isDevelopment
        ? pwd2("./node_modules/angular-loader/angular-loader.js")
        : pwd2("./node_modules/angular-loader/angular-loader.min.js"),
    },
  },
  plugins: [
    new rspack.DllPlugin({
      // name 必须和 output.library 一致
      name: "[name]-[hash]",
      // 该属性需要与 DllReferencePlugin 中一致
      context: __dirname,
      path: path.join(__dirname, "prebundle", "[name]-manifest.json"),
    }),
  ],
};
